<template>
	<div>
		<el-button icon="el-icon-back" @click="back()">返回试卷</el-button>
		<el-container
			style="height: auto; border: 1px solid #eeel;margin-top: 10px;box-shadow: 0px 0px 5px 2px gainsboro;">
			<el-aside width="200px" style="background-color: rgb(238, 241, 246)">
				<!-- <el-menu :default-openeds="['1']" :default-active="'1'">
					<el-submenu index="1">
						<template slot="title"><i class="el-icon-s-management"></i>目录</template>
						<div @click="scrollToElement(item.id)" v-for="(item,index) in a">
							<el-menu-item :index="`${String(index)}`">
								<i class="el-icon-s-order"></i>
								<span slot="title">{{index + 1}},{{item.type}} 分值{{item.score}}</span>
							</el-menu-item>
						</div>
					</el-submenu>
				</el-menu> -->
				<el-menu default-active="999" :default-openeds="['999']" class="el-menu-vertical-demo">
					<el-submenu index="999">
						<template slot="title">
							<i class="el-icon-s-management"></i>
							<span>目录</span>
						</template>
						<el-submenu :index="`${String(index)}`"
							v-for="(item,index) in AllQuestionList">
							<template slot="title">{{numberToChinese(index + 1)}}、{{item.type}} 共<span
									style="color: red;">{{item.point}}</span>分</template>
							<el-menu-item :index="`${String(index)}`+ '-' +`${String(num)}`"
								v-for="(j,num) in item.questionList" @click="scrollToElement(j.id)">{{num + 1}},{{j.type}} 分值{{j.score}}</el-menu-item>
						</el-submenu>
					</el-submenu>
				</el-menu>
			</el-aside>
			<el-container>
				<el-main>
					<div class="contantBox">
						<div class="botBox">
							<div style="color: red;">
								*添加完题目请点击保存试卷
							</div>
							<div style="display: flex;flex-direction: row;">
								<div>
									<el-button type="primary" @click="dialogFormVisible = true">试卷参数</el-button>
								</div>
								<div>
									<el-button type="success" @click="saveExamPaper()">保存试卷</el-button>
								</div>
								<div>
									<el-button type="primary" @click="look()">预览试卷</el-button>
								</div>
							</div>
						</div>
						<div class="titleBox">
							<el-input placeholder="请输入内容" size="medium" disabled v-model="paperName"
								style="font-size: 18px;text-align: center;">
								<template slot="prepend">试卷名称</template>
							</el-input>
							<div style="text-align: center;font-size: 14px;margin:10px 0px 20px 0px;">
								试卷说明：本试卷共包含<span style="color: red;">{{AllQuestionList.length}}</span>个大题，<span
									style="color: red;">{{examNumber}}</span>道小题； 试卷分值<span
									style="color: red;">{{updateList.score}}</span>分，
								作答时间<span style="color: red;">{{updateList.examTime}}</span>分钟，至少<span
									style="color: red;">{{updateList.handTime}}</span>分钟后，可以交卷</div>
						</div>
						<div class="paperContont">
							<div class="intoBigQuestionsBox" v-for="(j,index) in AllQuestionList">
								<div class="bigQuestionsBox">
									<div style="display: flex;flex-direction: row;align-items: center;">
										<div style="font-size: 18px;">{{numberToChinese(index + 1)}}、</div>
										<div>
											<el-input placeholder="请输入内容" disabled v-model="j.type"
												style="font-size: 18px;text-align: center;">
												<template slot="prepend">大题名称</template>
											</el-input>
										</div>
									</div>
									<div style="font-size: 14px;">
										共计<span style="color: red;">{{j.num}}</span>道小题，共<span
											style="color: red;">{{j.point}}</span>分
									</div>
									<div style="color: #409EFF;cursor: pointer;" @click="deleteBigQuestion(index)">
										移除大题
									</div>
								</div>
								<div class="topicBox" v-for="(item,index) in j.questionList">
									<div style="background-color: #faf9fe;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"
										:id="item.id">
										<div class="topicTitle">
											<div>{{index + 1}}、{{item.name}}
												<el-tag size="medium">分值:{{item.score}}分</el-tag>
											</div>
											<div style="color:#409EFF;cursor: pointer;font-size: 14px;width: 70px;margin-left: 10px;" @click="changePoint(item.id)">
												修改分值
											</div>
										</div>
										<div style="padding: 10px 20px 10px 20px;cursor: pointer;"
											@click="deleteExam(item.id)">
											<i class="el-icon-delete" title="删除"></i>
										</div>
									</div>
									<div class="topicConBox">
										<!-- <div class="topicWord">{{item.name}}</div> -->
										<div class="checkBoxList" v-if="item.type == '单项选择题'">
											<el-radio-group v-model="item.answer">
												<div v-for="j in item.options">
													<el-radio :label="j.value" border size="medium"
														:disabled='j.value != item.answer'>{{j.label}}.{{j.value}}</el-radio>
												</div>
											</el-radio-group>
										</div>
										<div class="checkBoxList" v-if="item.type == '多项选择题'">
											<el-checkbox-group v-model="item.answer">
												<div v-for="j in item.options">
													<el-checkbox :label="j.value" border :disabled='!j.ischeck'
														size="medium">{{j.label}}.{{j.value}}</el-checkbox>
												</div>
											</el-checkbox-group>
										</div>

										<div class="checkBoxList" v-if="item.type == '判断题'">
											<el-radio-group v-model="item.answer">
												<div v-for="j in tfOptions">
													<el-radio :label="j.name" border size="medium"
														:disabled='j.name != item.answer'>{{j.name}}</el-radio>
												</div>
											</el-radio-group>
										</div>
										<div class="inputBox" v-if="item.type == '填空题' || item.type == '简答题'">
											<div style="font-size: 15px;width:80px;">正确答案:</div>
											<el-input type="textarea" :rows="3" placeholder="请输入内容" disabled
												v-model="item.answer">
											</el-input>
										</div>
									</div>
									<div class="checkBoxList">
										<el-collapse accordion>
											<el-collapse-item title="查看解析">
												<div>{{item.analysis}}</div>
												<!-- <div v-if="item.analysis == null">暂无解析</div> -->
											</el-collapse-item>
										</el-collapse>
									</div>
								</div>
								<div style="width: 95%;margin: 20px auto;">
									<el-button type="primary" size="medium" style="width: 100%;"
										@click="searchForm.type = j.type,addQuestion()">添加题目</el-button>
								</div>
							</div>
						</div>
						<div style="width: 95%;margin: 20px auto;">
							<el-button size="medium" style="width: 100%;" @click="addBigQuestion = true">添加大题
							</el-button>
						</div>
					</div>
				</el-main>
			</el-container>
		</el-container>
		<!-- 预览试卷 -->
		<el-dialog title="预览试卷" :visible.sync="lookExamPaperDialog" width="60%">
			<div class="viewPaperContont">
				<div class="intoBigQuestionsBox" v-for="(j,index) in AllQuestionList">
					<div class="bigQuestionsBox">
						<div style="display: flex;flex-direction: row;align-items: center;">
							<div style="font-size: 18px;">{{numberToChinese(index + 1)}}、</div>
							<div>
								<el-input placeholder="请输入内容" disabled v-model="j.type"
									style="font-size: 18px;text-align: center;">
									<template slot="prepend">大题名称</template>
								</el-input>
							</div>
						</div>
						<div style="font-size: 14px;">
							共计<span style="color: red;">{{j.num}}</span>道小题，共<span
								style="color: red;">{{j.point}}</span>分
						</div>
						<div style="color: #409EFF;cursor: pointer;" @click="deleteBigQuestion(index)">
							
						</div>
					</div>
					<div class="topicBox" v-for="(item,index) in j.questionList">
						<div style="background-color: #faf9fe;width: 100%;display: flex;flex-direction: row;justify-content: space-between;"
							:id="item.id">
							<div class="topicTitle">
								<div>{{index + 1}}、{{item.name}}</div>
								<div class="scoreBox">
									<el-tag size="medium">分值:{{item.score}}分</el-tag>
								</div>
							</div>
							
						</div>
						<div class="topicConBox">
							<!-- <div class="topicWord">{{item.name}}</div> -->
							<div class="checkBoxList" v-if="item.type == '单项选择题' || item.type == '多项选择题'">
								<el-radio-group>
									<div v-for="j in item.options">
										<el-radio :label="j.label" border size="medium">{{j.label}}.{{j.value}}</el-radio>
									</div>
								</el-radio-group>
							</div>
							<!-- <div class="checkBoxList" v-if="item.type == '多项选择题'">
								<el-checkbox-group>
									<div v-for="j in item.options">
										<el-checkbox :label="j.label" border size="medium">{{j.label}}.{{j.value}}</el-checkbox>
									</div>
								</el-checkbox-group>
							</div> -->

							<div class="checkBoxList" v-if="item.type == '判断题'">
								<el-radio-group>
									<div v-for="j in tfOptions">
										<el-radio :label="j.name" border size="medium">{{j.name}}</el-radio>
									</div>
								</el-radio-group>
							</div>
							<div class="inputBox" v-if="item.type == '填空题' || item.type == '简答题'">
								<div style="font-size: 15px;width:80px;">正确答案:</div>
								<el-input type="textarea" :rows="3" placeholder="请输入内容">
								</el-input>
							</div>
						</div>
					</div>
				</div>
			</div>
		</el-dialog>
		<!-- 修改分值 -->
		<el-dialog title="修改分值" :visible.sync="pointFormVisible" width="20%">
			<el-form :model="getAnswerPonit">
				<el-form-item label="题目分值:" prop="score">
					<el-input v-model="getAnswerPonit.score">
					</el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="pointFormVisible = false">取 消</el-button>
				<el-button type="primary" @click="putPoint()">确 定</el-button>
			</div>
		</el-dialog>
		<!-- 试卷参数 -->
		<el-dialog title="试卷参数" :visible.sync="dialogFormVisible" width="20%">
			<el-form :model="updateList" :rules="rules" ref="updateList">
				<el-form-item label="考试时长:" prop="examTime">
					<el-input v-model="updateList.examTime">
						<template slot="append">min</template>
					</el-input>
				</el-form-item>
				<el-form-item label="交卷时间:" prop="handTime">
					<el-input v-model="updateList.handTime">
						<template slot="append">min</template>
					</el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="quxiao()">取 消</el-button>
				<el-button type="primary" @click="addParameter()">确 定</el-button>
			</div>
		</el-dialog>
		<!-- 添加大题 -->
		<el-dialog title="添加大题" :visible.sync="addBigQuestion" width="30%">
			<div class="BigQuestionBox">
				<div style="margin-right: 10px;font-size: 16;font-weight: bold;">题型</div>
				<el-select v-model="searchForm.type" filterable placeholder="请选择大题题型" clearable>
					<el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.name">
					</el-option>
				</el-select>
			</div>
			<div slot="footer" class="dialog-footer">
				<el-button @click="addBigQuestion = false">取 消</el-button>
				<el-button type="primary" @click="sure()">确 定</el-button>
			</div>
		</el-dialog>
		<!-- 添加试题 -->
		<el-dialog title="添加题目" :visible.sync="dialogTableVisible" width="50%">
			<div style="min-height: 150px;">
				<div class="searchBox">
					<div class="questionBox">
						<div style="margin-right: 10px;font-size: 16;font-weight: bold;">题库</div>
						<el-select v-model="searchForm.bankId" filterable placeholder="请选择题库" clearable
							@change="getPageList()">
							<el-option v-for="item in bankList" :key="item.bankId" :label="item.name"
								:value="item.bankId">
							</el-option>
						</el-select>
					</div>
					<div class="questionBox">
						<div style="margin-right: 10px;font-size: 16;font-weight: bold;">题型</div>
						<el-select v-model="searchForm.type" filterable placeholder="请选择题型" clearable
							@change="getPageList()" disabled>
							<el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.name">
							</el-option>
						</el-select>
					</div>
					<div class="questionBox">
						<div style="margin-right: 10px;font-size: 16;font-weight: bold;width: 40px;">搜索</div>
						<el-input placeholder="请输入题干搜索内容" v-model="searchForm.name" clearable @blur='getPageList()'>
						</el-input>
					</div>
				</div>
				<div style="width: 95%;margin:20px auto;">
					<el-table :data="listData" ref="multipleTable" style="width: 100%" @select="handleSelect"
						@select-all="aa" :header-cell-style="headClass" :cell-style="rowClass">
						<el-table-column type="selection" width="45"></el-table-column>
						<el-table-column prop="type" label="题型" width="80">
						</el-table-column>
						<el-table-column prop="name" label="题目" show-overflow-tooltip>
						</el-table-column>
						<el-table-column prop="score" label="分值" width="60">
						</el-table-column>
					</el-table>
				</div>
				<div style="margin-top: 20px;display: flex;flex-direction: row;justify-content:center;">
					<el-pagination @current-change="currentChange" @size-change="sizeChange"
						:page-size="pageInfo.pageSize" :current-page="pageInfo.currentPage" background
						layout="sizes, prev, pager, next" :total="total">
					</el-pagination>
				</div>
				<div slot="footer" class="dialog-footer"
					style="display: flex;flex-direction: row;justify-content: space-between;width: 60%;margin: 10px auto;">
					<el-button @click="reset()">取 消</el-button>
					<el-button type="primary" @click="addTopic">确 定</el-button>
				</div>
			</div>
		</el-dialog>
	</div>
</template>

<style scoped>
	.el-header {
		background-color: #B3C0D1;
		color: #333;
		line-height: 60px;
	}

	a {
		text-decoration: none;
	}

	.intoBigQuestionsBox {
		margin: 20px auto;
		width: 95%;
		border: solid 1px #DCDFE6;
	}

	.BigQuestionBox {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
	}


	.questionBox {
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.bigQuestionsBox {
		border: solid 1px #DCDFE6;
		padding: 10px 20px;
		display: flex;
		flex-direction: row;
		width: 100%;
		margin: 0 auto;
		justify-content: space-between;
		align-items: center;
	}

	.searchBox {
		width: 95%;
		margin: 0 auto;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.checkBoxList {
		padding: 10px 20px 10px 20px;
	}

	.inputBox {
		padding: 10px 20px 10px 20px;
		display: flex;
		flex-direction: row;
		margin: 10 auto;
	}

	.paperContont {
		/* min-width: 400px; */
		max-height: calc(100vh - 450px);
		overflow-y: scroll;
	}

	.viewPaperContont {
		/* min-width: 400px; */
		max-height: calc(100vh - 200px);
		overflow-y: scroll;
	}

	::-webkit-scrollbar {
		width: 4px;
		/*  设置纵轴（y轴）轴滚动条 */
		height: 4px;
		/*  设置横轴（x轴）轴滚动条 */
	}

	/* 滚动条滑块（里面小方块） */
	::-webkit-scrollbar-thumb {
		border-radius: 10px;
		box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
		background: #409EFF;
	}

	/* 滚动条轨道 */
	::-webkit-scrollbar-track {
		border-radius: 0;
		box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
		background: gainsboro;
	}

	.el-radio-group {
		width: 100%;
	}

	.el-radio-group .el-radio:last-child {
		width: 100%;
		margin-bottom: 10px;
	}

	.el-checkbox.is-bordered.el-checkbox--mini {
		width: 100%;
		margin-bottom: 10px;
	}

	.topicConBox {
		width: 100%;
	}

	.topicWord {
		padding: 10px 20px 10px 20px;
	}

	.topicBox {
		width: 95%;
		margin: 20px auto;
		border: solid 1px gainsboro;
		display: flex;
		flex-direction: column;
	}

	.scoreBox {
		margin: 0px 10px 0px 10px;
	}

	.topicTitle {
		display: flex;
		flex-direction: row;
		align-items: center;
		padding: 10px 20px 10px 20px;
	}

	.botBox {
		width: 95%;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		margin: 10px auto;

	}

	.botBox div {
		margin: 0px 8px 0px 8px;

	}

	.titleBox {
		width: 95%;
		margin: 0 auto;
		text-align: center;

	}

	.contantBox {
		width: 100%;
		height: calc(100vh - 250px);
	}

	.el-aside {
		color: #333;
	}
</style>

<script>
	import utils from "@/utils/utils";
	export default {
		data() {
			return {
				isDrawerDialog: false,
				paperName: '',
				input: '',
				listData: [],
				pointFormVisible:false,
				dialogFormVisible: false,
				dialogQusetionsVisible: false,
				lookExamPaperDialog: false,
				dialogTableVisible: false,
				addBigQuestion: false,
				dialogCanshu: false,
				examNumber: 0,
				total: 0,
				chooseID: 0,
				rules: {
					examTime: [{
						required: true,
						message: '请输入考试时长',
						trigger: 'blur'
					}],
					handTime: [{
						required: true,
						message: '请输入交卷时间',
						trigger: 'blur'
					}]
				},
				searchValue: '',
				radio: 3,
				title: '',
				tfOptions: [{
						id: 1,
						name: '对',
					},
					{
						id: 2,
						name: '错',
					},
				],
				options: [{
						id: 1,
						name: '单项选择题',
					},
					{
						id: 2,
						name: '多项选择题',
					},
					{
						id: 3,
						name: '判断题',
					},
					{
						id: 4,
						name: '填空题',

					},
					{
						id: 5,
						name: '简答题',
					}
				],
				questionList: [],
				topicList: [],
				//分页详情
				pageInfo: {
					//当前页
					pageNum: 1,
					//分页大小
					pageSize: 10
				},
				//搜索
				searchForm: {
					name: "",
					type: '',
					bankId: ''
				},
				getAnswerPonit:{},
				AllQuestionList: [],
				bankList: [],
				chooseTypeList: [],
				updateList: {
					examTime: '',
					handTime: '',
					id: '',

				},
				formData: {

				},
				//删除数据的id
				ids: [],
				tableData: [],
				userInfo: utils.getUserInfo(),
				chooseArr: [],
				selectedIds: [],
				pointNum:0,
				change: false,
				questionType: [
					"单项选择题",
					"多项选择题",
					"判断题",
					"填空题",
					"简答题"
				],
				selectList: [
					"A：",
					"B：",
					"C：",
					"D：",
				],
				nextLetter: 'E'.charCodeAt(0),
				defaultList: [
					"A：",
					"B：",
					"C：",
					"D：",
				],
			}
		},
		created() {
			this.paperName = this.$route.query.name
			this.updateList.id = this.$route.query.id
			this.getExamPaper()
			this.getQuestionBankList()
		},
		mounted() {
					// this.paperName = this.$route.query.name
					// this.updateList.id = this.$route.query.id
					// this.getExamPaper()
					// this.getQuestionBankList()
		},
		computed: {
			sortedSelectList() {
				return this.selectList;
			}
		},
		methods: {
			getExamPaper() {
				this.chooseArr = []
				this.$axios.get("/examPaper/getById?id=" + this.updateList.id).then(res => {
					console.log(213, res)
					this.updateList = res.data
					if (this.updateList.question != null) {
						this.AllQuestionList = JSON.parse(this.updateList.question)
						console.log(this.AllQuestionList,9527)
						this.AllQuestionList.forEach(item => {
							item.point = 0
							item.questionList.forEach(j => {
								item.point += j.score
								if(item.type == '单项选择题' || item.type == '多项选择题'){
									console.log(j.options,'阿斯顿')
									// j.options = JSON.parse(j.options)
								}
								this.chooseArr.push(j)
							})
						})

						// this.chooseArr = this.updateList.question
						this.examNumber = this.chooseArr.length

					}

					this.toggleSelection()
					console.log(this.updateList, '查询')
				})
			},
			putPoint(){
				this.AllQuestionList.forEach(item=>{
					item.questionList.forEach(j=>{
						if(j.id == this.pointNum){
							j.score = parseInt(this.getAnswerPonit.score)
						}
					})
				})
				this.updateList.question = JSON.stringify(this.AllQuestionList)
				this.updateList.score = 0
				this.chooseArr.forEach(item => {
					this.updateList.score += item.score
				})
				this.$axios.put("/examQuestion/update", this.getAnswerPonit).then(res => {
					
				})
				this.$axios.put("/examPaper/update", this.updateList).then(res => {
					console.log('保存', res)
					if (res.code == 200) {
						this.pointFormVisible = false
						this.getExamPaper()
						this.$message.success("保存成功!")
					} else {
						this.$message.error('保存失败')
					}
				})
				
			},
			look(){
				this.lookExamPaperDialog = true
			},
			changePoint(id){
				this.pointNum = id
				console.log(this.AllQuestionList)
				this.pointFormVisible = true
				this.$axios.get("/examQuestion/getById?id=" + id).then(res => {
					console.log(res,999)
					this.getAnswerPonit = res.data
				})
			},
			
			addQuestion() {
				this.getPageList()
				this.dialogTableVisible = true
			},
			numberToChinese(num) {
				const chineseNums = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
				const chineseUnits = ['', '十', '百', '千', '万', '十万', '百万', '千万', '亿'];

				if (typeof num !== 'number' || isNaN(num)) {
					return '输入无效';
			 }

				if (num === 0) {
					return chineseNums[0];
				}

				let strNum = num.toString();
				let len = strNum.length;
				let chineseStr = '';
				let zeroFlag = false;

				for (let i = 0; i < len; i++) {
					let digit = parseInt(strNum[i]);
					let unit = chineseUnits[len - 1 - i];

					if (digit === 0) {
						if (!zeroFlag) {
							if (chineseStr && chineseStr !== chineseNums[0]) {
								zeroFlag = true;
								chineseStr += chineseNums[0];
							}
						}
					} else {
						zeroFlag = false;
						chineseStr += chineseNums[digit] + unit;
					}

					if (unit === '十' && i < len - 1 && parseInt(strNum[i + 1]) === 0) {
						chineseStr = chineseStr.slice(0, -1); // 去掉 '十' 后面的 '零'
					}

					if (unit === '万' || unit === '亿') {
						if (chineseStr.endsWith(chineseNums[0])) {
							chineseStr = chineseStr.slice(0, -1); // 去掉以 '万' 或 '亿' 结尾的 '零'
						}
						chineseStr += unit;
					}
				}

				// 处理特殊情况，如 "10010" 转换为 "一万零一十" 而不是 "一万零零一十"
				chineseStr = chineseStr.replace(/零+$/, ''); // 去掉末尾的零
				chineseStr = chineseStr.replace(/零+([一二三四五六七八九])/g, '$1'); // 连续零只保留一个
				chineseStr = chineseStr.replace(/零+万/g, '万'); // 去掉 "万" 前的零
				chineseStr = chineseStr.replace(/零+亿/g, '亿'); // 去掉 "亿" 前的零
				chineseStr = chineseStr.replace(/^一十/, '十'); // 去掉 "十" 前的 "一"
			 return chineseStr;
			},
			sure() {
				let a = {
					type: this.searchForm.type,
					num: 0,
					point: 0,
					questionList: []
				}
				this.AllQuestionList.push(a)
				this.searchForm.type = ''
				this.addBigQuestion = false
				console.log(this.AllQuestionList)
			},
			scrollToElement(elementId) {
				console.log(elementId)
				const element = document.getElementById(elementId);
				if (element) {
					element.scrollIntoView({
						behavior: 'smooth'
					});
				}
			},
			saveExamPaper() {
				if (parseInt(this.updateList.examTime) < parseInt(this.updateList.handTime)) {
					this.$message.error('提前交卷时间不能大于考试时间！')
					return
				}
				this.selectedIds = []
				if (this.updateList.examTime == null || this.updateList.handTime == null) {
					this.$message.error('请输入试卷参数')
					setTimeout(() => {
						this.dialogFormVisible = true
					}, 1000)
					return
				}
				this.updateList.question = JSON.stringify(this.AllQuestionList)
				if (this.chooseArr.length != 0) {
					this.chooseArr.forEach(item => {
						this.selectedIds.push(item.id)

					})
				}
				this.updateList.questionId = this.selectedIds.join(',')
				console.log(this.updateList, '上传')
				this.$axios.put("/examPaper/update", this.updateList).then(res => {
					console.log('保存', res)
					if (res.code == 200) {
						this.getExamPaper()
						this.$message.success("保存成功!")
					} else {
						this.$message.error('保存失败')
					}
				})
			},
			addOption() {
				// 生成下一个字母的字符  
				const nextChar = String.fromCharCode(this.nextLetter);
				// 创建新选项文本  
				const newOption = `${nextChar}：`;
				// 将新选项添加到数组中  
				this.selectList.push(newOption);
				// 更新下一个字母的字符编码（注意：这里简单处理到Z后不再递增，你可以根据需要添加逻辑处理Z之后的情况）  
				this.nextLetter = (this.nextLetter === 'Z'.charCodeAt(0)) ? 'A'.charCodeAt(0) : this.nextLetter + 1;
				// 如果需要确保数组始终有序（虽然在这个例子中不需要），可以取消注释下面的代码：  
				// this.selectList.sort((a, b) => a.localeCompare(b));  
			},
			addParameter() {
				console.log(this.updateList.examTime, this.updateList.handTime)
				if (parseInt(this.updateList.examTime) < parseInt(this.updateList.handTime)) {
					this.$message.error('提前交卷时间不能大于考试时间！')
					return
				}
				this.dialogFormVisible = false
				// this.$refs[formName].validate((valid) => {
				// 	if (valid) {
				// 		console.log(123,this.updateList)
				// 		this.updateList.question = JSON.stringify(this.updateList.question)
				// 		this.$axios.put("/examPaper/update", this.updateList).then(res => {
				// 			console.log('修改', res)
				// 			if (res.code == 200) {
				// 				this.getExamPaper()
				// 				this.$message.success("修改成功!")
				// 				this.dialogFormVisible = false
				// 			} else {
				// 				this.$message.error('修改失败')
				// 			}
				// 		})
				// 	} else {
				// 		return false;
				// 	}
				// });

			},
			getQuestionBankList() {
				let data = Object.assign(this.searchForm, this.pageInfo)
				this.$axios.get("/examQuestionBank/page", {
					params: data
				}).then(res => {
					let data = res.data.data
					data.forEach((item, index) => {
						this.bankList.push({
							bankId: item.id,
							name: item.name
						})
					})
				})
			},
			getPageList() {
				let data = Object.assign(this.searchForm, this.pageInfo)
				this.$axios.get("/examQuestion/page", {
					params: data
				}).then(res => {
					this.listData = res.data.list
					console.log(this.listData,'ABCD')
					this.total = res.data.total
					this.listData.forEach(item => {
						if (item.type == '单项选择题') {
							item.options = JSON.parse(item.options)
						} else if (item.type == '多项选择题') {
							item.options = JSON.parse(item.options)
							item.answer = JSON.parse(item.answer)
							item.options.forEach(j => {
								if (item.answer.indexOf(j.value) != -1) {
									j.ischeck = true
								} else {
									j.ischeck = false
								}
							})
						}

					})
					this.toggleSelection()
				})

			},
			quxiao() {
				this.dialogFormVisible = false
				this.$refs['updateList'].resetFields();
				this.updateList.examTime = ''
				this.updateList.handTime = ''
			},
			transformedArray(option) {
				return option.map(item => {
					// 将每个字符串按“：”分割成两部分  
					const [label, value] = item.split(':');
					// 返回一个对象，包含 label 和 value 属性  
					return {
						label,
						value
					};
				});
			},
			//取消添加
			reset(formName) {
				this.dialogTableVisible = false
				if (this.updateList.length == 0) {
					this.$refs.multipleTable.clearSelection();
					this.chooseArr = []

				}
			},
			//添加题目
			addTopic() {
				//选择题目时修改提示文字的题数和分数
				this.examNumber = this.chooseArr.length
				this.updateList.score = 0
				this.chooseArr = this.chooseArr.filter(item => item !== undefined);
				console.log(this.chooseArr, '添加的题目')
				this.chooseArr.forEach(item => {
					this.updateList.score += item.score

					this.AllQuestionList.forEach(j => {

						if (item.type == j.type) {
							j.questionList.push(item)
							//数组去重
							j.questionList = [...new Set(j.questionList)]
						}
					})

				})
				//计算每道大题的小题分数
				this.AllQuestionList.forEach(item => {
					item.point = 0
					item.questionList.forEach(j => {
						item.point += j.score
					})
					item.num = item.questionList.length
				})
				this.dialogTableVisible = false
				// console.log(this.AllQuestionList,'aaa')
				// console.log(123, this.updateList.score)
				let arr = JSON.stringify(this.AllQuestionList)
				this.updateList.question = JSON.parse(arr)
				this.dialogTableVisible = false

			},
			toggleSelection() {
				this.$nextTick(() => {
					try {
						this.chooseArr.forEach((row) => {
							this.$refs.multipleTable.toggleRowSelection(this.listData.find((item) => item
								.id == row.id), true);
						});
					} catch (error) {
						console.log("111", error);
					}
				});
			},
			aa(val) {
				if (val.length == 0) {
					this.chooseArr = []
				} else {
					this.chooseArr = val
				}

			},
			handleSelect(val, row) {
				console.log('val', val, row)
				let isSelected = val.some(selectedRow => selectedRow === row);
				console.log(isSelected)
				if (isSelected == true) {
					this.chooseArr.push(row)
				} else {
					this.chooseArr = this.chooseArr.filter(item => item.id !== row.id);
				}
				console.log(this.chooseArr, '最终')
			},
			handleChange(val) {
				console.log(val);
			},
			back() {
				this.$router.push({
					path: '/examPaperManage',
				})
			},
			resetForm(formName) {
				this.$refs['formName'].resetFields();
			},
			headClass() { //表头居中显示
				return "text-align:center;font-size:14px;background-color:#5eb7e8;color:white;border: solid 1px white;"
			},
			rowClass() { //表格数据居中显示
				return "text-align:center;font-size:13px;border:1px solid #e6ebf5;"
			},
			//选择分页
			currentChange(e) {
				this.pageInfo.pageNum = e
				this.getPageList()
			},
			//改变分页数量
			sizeChange(e) {
				this.pageInfo.pageSize = e
				this.getPageList()
			},
			//搜索
			handleSearch() {
				this.pageInfo.pageNum = 1
				this.getPageList()
			},
			//单个删除
			handleDelete(index, row) {
				this.batchDelete([row])
			},
			//批量删除
			deleteExam(id) {
				console.log(id)
				
				this.$confirm(`此操作将删除本条试题, 是否继续?`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning',
					center: true
				}).then(() => {
					this.chooseArr = this.chooseArr.filter(item => item.id !== id);
					// this.updateList.question = this.updateList.question.filter(item => item.id !== id);
					this.AllQuestionList.forEach(item => {
						
						item.questionList = item.questionList.filter(item => item.id !== id)
					})
					console.log(this.AllQuestionList,'删除')
					this.examNumber = this.chooseArr.length
					this.updateList.score = 0
					this.chooseArr.forEach(item => {
						this.updateList.score += item.score
					})
					this.toggleSelection()
					// this.getExamPaper()
					this.$message.success('删除成功')
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
			deleteBigQuestion(index) {
				console.log(index)
				this.$confirm(`此操作将删除本条试题, 是否继续?`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning',
					center: true
				}).then(() => {
					this.chooseArr = []
					this.AllQuestionList.splice(index, 1);
					this.AllQuestionList.forEach(item => {
						item.questionList.forEach(j => {
							this.chooseArr.push(j)
						})
					})
					this.examNumber = this.chooseArr.length
					this.updateList.score = 0
					this.chooseArr.forEach(item => {
						this.updateList.score += item.score
					})
					this.toggleSelection()
					this.$message.success('删除成功')
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
			//添加数据
			handleAdd() {
				this.formData = {}
				this.isDrawerDialog = true
				// 重置selectList为初始选项  
				this.selectList = [...this.defaultList];
				// 重置nextLetter为E（如果需要从E开始添加新选项）  
				this.nextLetter = 'E'.charCodeAt(0);
			},

		}
	};
</script>
